﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.ApproveCommand;
using B.S.DataBase.MyWrite.API.Appicant.Command;
using MediatR;
using System.Diagnostics;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.ApproveCommandHandler
{

    public class DelLeaveModelCommandHandler : IRequestHandler<DelLeaveModelCommand, APIResult<string>>
    {
        private readonly IBaseRepository<LeaveModel> leaveModelReppos;
        private readonly IMapper mapper;

        public DelLeaveModelCommandHandler(IBaseRepository<LeaveModel> LeaveModelReppos, IMapper mapper)
        {
            leaveModelReppos = LeaveModelReppos;
            this.mapper = mapper;
        }


        public  Task<APIResult<string>> Handle(DelLeaveModelCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            var obj = leaveModelReppos.GetAll().Result.FirstOrDefault(x => x.LeaveId == request.LeaveId);
            obj.IsDeleted = true;
            var res = leaveModelReppos.Update(obj).Result;
            if (res == 1)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res.ToString();
            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                result.data = res.ToString();
            }
            return  Task.FromResult(result);
        }
    }
    }
